package com.gykfbxxh.imovie.bean.news;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.bean.film.FilmListEntity;
import com.gykfbxxh.imovie.utils.glide.ImageLoader;
import com.gykfbxxh.imovie.videoplayer.JCUserAction;
import com.gykfbxxh.imovie.videoplayer.JCVideoPlayer;
import com.gykfbxxh.imovie.videoplayer.JCVideoPlayerStandard;
import com.xiao.nicevideoplayer.NiceVideoPlayer;

import java.util.ArrayList;
import java.util.List;


/**
 * 新闻列表的视频
 * Created by xuxinghai on 2016/7/17.
 */
public class ViewFilmsVideo implements JCUserAction {

    public JCVideoPlayerStandard item_videoplayer;
    public NiceVideoPlayer nice_video_player;
    public RelativeLayout rl_autoview;
    public ImageView iv_leftsmallimg;
    public ViewFlipper viewflipper;
    public TextView tv_title;
    public TextView tv_type,tv_score;
    public Context context;
    List<String> data = new ArrayList<>();

    public ViewFilmsVideo(View convertView) {
        initView(convertView);
    }

    public ViewFilmsVideo(View convertView, Context context) {
        this.context =context;
        initView(convertView);
    }


    /**
     * 初始化控件
     * @param convertView
     */
    public void initView(View convertView) {
        item_videoplayer = (JCVideoPlayerStandard) convertView.findViewById(R.id.item_videoplayer);
        item_videoplayer.setJcUserAction(this);
//        nice_video_player = (NiceVideoPlayer) convertView.findViewById(R.id.nice_video_player);
//        nice_video_player.setPlayerType(NiceVideoPlayer.TYPE_IJK);
//        ViewGroup.LayoutParams params = nice_video_player.getLayoutParams();
//        params.width = convertView.getResources().getDisplayMetrics().widthPixels; // 宽度为屏幕宽度
//        params.height = (int) (params.width * 9f / 16f);    // 高度为宽度的9/16
//        nice_video_player.setLayoutParams(params);
        viewflipper = (ViewFlipper) convertView.findViewById(R.id.viewflipper);
        rl_autoview = (RelativeLayout) convertView.findViewById(R.id.rl_autoview);
        iv_leftsmallimg = (ImageView) convertView.findViewById(R.id.iv_leftsmallimg);
        tv_title = (TextView) convertView.findViewById(R.id.tv_title);
        tv_type = (TextView) convertView.findViewById(R.id.tv_type);
        tv_score = (TextView) convertView.findViewById(R.id.tv_score);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     * @param listBean
     */
    public void loadData(FilmListEntity listBean, int typeFlag){
        FilmEntity filmEntity = listBean.getFObject();
        tv_title.setText(listBean.getFName());
        ImageLoader.getInstance().listLoadImage2Cache(context,iv_leftsmallimg,listBean.getFObject().getFPhotos());
        tv_title.setText(listBean.getFName());
        tv_type.setText(listBean.getFObject().getFName());
        tv_score.setText(filmEntity.getFScore()+"");

        if(TextUtils.isEmpty(filmEntity.getFSourceurl())) {
            return;
        }

//        XxhVideoPlayerController controller = new XxhVideoPlayerController(context);
//        controller.setTitle(filmEntity.getFName());
//        ImageLoader.getInstance().listLoadImage2Cache(context,controller.imageView(),filmEntity.getFPhotow());
//        nice_video_player.setController(controller);
//        nice_video_player.setUp(filmEntity.getFSourceurl(), null);

//        List<Clarity> clarities = new ArrayList<>();
//        clarities.add(new Clarity("标清", "270P", filmEntity.getFSourceurl()));
//        clarities.add(new Clarity("高清", "270P", filmEntity.getFSourceurl()));
//        XxhVideoPlayerController controller = new XxhVideoPlayerController(context);
//        controller.setTitle(filmEntity.getFName());
//        controller.setLenght(0);
//        controller.setClarity(clarities, 0);
//        ImageLoader.getInstance().loadImage2Cache(context,controller.imageView(),filmEntity.getFPhotow());
//        nice_video_player.setController(controller);
        //加载视频
        if(!TextUtils.isEmpty(filmEntity.getFPhotow())){
            item_videoplayer.setUp(filmEntity.getFSourceurl(), JCVideoPlayer.SCREEN_LAYOUT_NORMAL,
                    filmEntity.getFName(), filmEntity.getFPhotow());
        }else{
            item_videoplayer.setUp(filmEntity.getFSourceurl(), JCVideoPlayer.SCREEN_LAYOUT_NORMAL,
                    filmEntity.getFName(), filmEntity.getFPhotow());
        }
//        data.clear();
//        data.add("好看好看真的很好看!");
//        data.add("大片!超级大片!");
//        data.add("精品!很不错的电影!");
//        data.add("看了一遍没看够");
//        data.add("不要疯狂的迷恋哥,哥只是个传说!");
//        data.add("下蛋公鸡,公鸡中的战斗机.");
//        if("3".equals(typeFlag)) {
//            viewflipper.setTag(R.id.tag_third,filmEntity.getFName());
////            viewflipper.setVisibility(View.VISIBLE);
//            ViewFlipperUtils.getInstance().setFlipperView(viewflipper,data, (Activity) context);
////            ViewFlipperUtils.getInstance().startFlipper();
//        }else {
//            viewflipper.setVisibility(View.GONE);
//            AnimationUtils.getInstance().autoScrollAnimation((Activity) context,data,rl_autoview);
//        }
    }

    @Override
    public void onEvent(int type, String url, int screen, Object... objects) {

    }

//    @Override
//    public void onEvent(int type, String url, int screen, Object... objects) {
//        LogUtils.e("xuxh","type="+type+" url="+url+" screen="+screen +" objects="+objects[0].toString());
//
//        if(viewflipper!=null) {
//            String playUrl = (String) viewflipper.getTag(R.id.tag_secong);
//            if(!TextUtils.isEmpty(playUrl)&&!TextUtils.isEmpty(url)) {
//                if(playUrl.equals(url)) {
//                    viewflipper.stopFlipping();
//                    viewflipper.setVisibility(View.GONE);
//                }
//            }
//        }
//    }
}
